package com.bqtt.tumor.demo.entity;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.tbwork.utility.toolbox.tbConvert;

import com.bqtt.tumor.demo.business.CityService;
import com.bqtt.tumor.demo.business.OccupationService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.presentation.controller.login;
 


/**
 * @author Administrator
 *
 */
@Entity
@Table(name="health_user") 
public class HealthUser  implements IDBEntity{

	
	// 以下为ORM字段
	
	@Id 
    @Column(name="health_user_id")    
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
	private Integer health_user_id;
	
	@Column(name = "id_number", length= 30)
	@Size(min=18,max=22,message="长度为18-22个数字")
	private String id_number;
	
	@Column(name = "name", length= 100)
	@Size(min=1,max=100,message="长度为1-100个汉字")
	private String name;
	
	@Column(name = "sexuality")
	private int sexuality;
	
	@Column(name = "birth_datetime")
	private Timestamp birth_datetime;

	@Column(name = "born_place_code")
	private int born_place_code;

	@Column(name = "residence_place")
	private int residence_place;

	@Column(name = "now_place")
	private int now_place;

	@Column(name = "occupation_code")
	private int occupation_code;

	
	// 以下为MVC字段
	@Transient
	private String birth_datetime_string=""; // for mvc
	@Transient
	private String born_place_string=""; // for mvc
	@Transient
	private String residence_place_string=""; // for mvc
	@Transient
	private String now_place_string=""; // for mvc
	@Transient
	private String occupation_string=""; // for mvc
	@Transient
	private String sexuality_string=""; // for mvc
	@Transient
	private String displayName=""; // for mvc
	

	
	public HealthUser(Integer health_user_id, String id_number, String name,
			int sexuality, Timestamp birth_datetime, int born_place_code,
			int residence_place, int now_place, int occupation_code) {
		super();
		this.health_user_id = health_user_id;
		this.id_number = id_number;
		this.name = name;
		this.sexuality = sexuality;
		this.birth_datetime = birth_datetime;
		this.born_place_code = born_place_code;
		this.residence_place = residence_place;
		this.now_place = now_place;
		this.occupation_code = occupation_code;
	}

	public HealthUser() {
		super();
		displayName="none";
		// TODO Auto-generated constructor stub
	}

	public Integer getHealth_user_id() {
		return health_user_id;
	}

	public void setHealth_user_id(Integer health_user_id) {
		this.health_user_id = health_user_id;
	}

	public String getId_number() {
		return id_number;
	}

	public void setId_number(String id_number) {
		this.id_number = id_number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSexuality() {
		return sexuality;
	}

	public void setSexuality(int sexuality) {
		this.sexuality = sexuality;
	}

	public Timestamp getBirth_datetime() {
		return birth_datetime;
	}

	public void setBirth_datetime(Timestamp birth_datetime) {
		this.birth_datetime = birth_datetime;
	}

	public int getBorn_place_code() {
		return born_place_code;
	}

	public void setBorn_place_code(int born_place_code) {
		this.born_place_code = born_place_code;
	}

	public int getResidence_place() {
		return residence_place;
	}

	public void setResidence_place(int residence_place) {
		this.residence_place = residence_place;
	}

	public int getNow_place() {
		return now_place;
	}

	public void setNow_place(int now_place) {
		this.now_place = now_place;
	}

	public int getOccupation_code() {
		return occupation_code;
	}

	public void setOccupation_code(int occupation_code) {
		this.occupation_code = occupation_code;
	}

	public String getBirth_datetime_string() {
		return birth_datetime_string;
	}

	public void setBirth_datetime_string(String birth_datetime_string) {
		this.birth_datetime_string = birth_datetime_string;
	}


	public String getNow_place_string() {
		return now_place_string;
	}

	public void setNow_place_string(String now_place_string) {
		this.now_place_string = now_place_string;
	}

	public String getOccupation_string() {
		return occupation_string;
	}

	public void setOccupation_string(String occupation_string) {
		this.occupation_string = occupation_string;
	}

	public String getBorn_place_string() {
		return born_place_string;
	}

	public void setBorn_place_string(String born_place_string) {
		this.born_place_string = born_place_string;
	}

	
	public String getResidence_place_string() {
		return residence_place_string;
	}

	public void setResidence_place_string(String residence_place_string) {
		this.residence_place_string = residence_place_string;
	}

	public String getSexuality_string() {
		return sexuality_string;
	}

	public void setSexuality_string(String sexuality_string) {
		this.sexuality_string = sexuality_string;
	}

	
	
	


	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**
	 *  把一些code编号 转变为实际的字符串
	 */
	public void fillStringField()
	{
		if(birth_datetime!=null)
		{
			birth_datetime_string = InlineTools.getDateString(tbConvert.Timestamp2String(birth_datetime));
		}
		
		
		// 出生地
		if(born_place_code!=0)
			born_place_string = CityService.getNameByCode(born_place_code);
		
		//长居住地
		if(residence_place!=0)
			residence_place_string = CityService.getNameByCode(residence_place);
		
		// 先居住地
		if(now_place!=0)
			now_place_string = CityService.getNameByCode(now_place);
		
		if(occupation_code!=0)
			occupation_string = OccupationService.getNameByCode(occupation_code);

		
		
		
		if(sexuality  == 0)
			sexuality_string = "男";
		else
			sexuality_string = "女";
		
		
	}
	
	public void fillDBfields()
	{
		if(!InlineTools.isEmpty(birth_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(birth_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				temp = new Timestamp(0);
			}
			
			birth_datetime =temp;
		}
		
		if(!InlineTools.isEmpty(born_place_string))
		{
			born_place_code = (int) CityService.getCodeByName(born_place_string);
		}


		
		if(!InlineTools.isEmpty(now_place_string))
		{
			now_place = (int) CityService.getCodeByName(now_place_string);
		}
		
		if(!InlineTools.isEmpty(occupation_string))
		{
			 occupation_code = (int) OccupationService.getCodeByName(occupation_string);
		}
		
		if(!InlineTools.isEmpty(residence_place_string))
		{
			 residence_place = (int) CityService.getCodeByName(residence_place_string);
		}
		
	}
	
	/**  获取SQL有效的非空的字段
	 * @return
	 */
	public Map<String,Object> getSQLValidAttributes()
	{
		Map<String,Object> hashMap = new HashMap<String,Object>();
		
		if(!InlineTools.isEmpty(birth_datetime_string))
		{
			Timestamp temp =null;
			try{
				temp= tbConvert.StringToTimestamp(birth_datetime_string+" 00:00:00");
				hashMap.put("birth_datetime", birth_datetime_string+" 00:00:00");
			}
			catch(Exception e)
			{
				;//do nothing
			}
			
			
		}
		
		if(!InlineTools.isEmpty(born_place_string))
		{
			hashMap.put("born_place_code", CityService.getCodeByName(born_place_string));
		}
		
		if(!InlineTools.isEmpty(id_number))
		{
			hashMap.put("id_number", id_number);
		}
		
		if(!InlineTools.isEmpty(name))
		{
			hashMap.put("name", name);
		}
		
		if(!InlineTools.isEmpty(now_place_string))
		{
			hashMap.put("now_place", CityService.getCodeByName(now_place_string));
		}
		
		if(!InlineTools.isEmpty(occupation_string))
		{
			hashMap.put("occupation_code", OccupationService.getCodeByName(occupation_string));
		}
		
		if(!InlineTools.isEmpty(residence_place_string))
		{
			hashMap.put("residence_place", CityService.getCodeByName(residence_place_string));
		}
		
		
		
		if(sexuality==0 || sexuality==1)
		{
			hashMap.put("sexuality", sexuality);
		}
		
		
		return hashMap;
	}
	
}
